package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.param.UpdateUserParam;
import cn.frontop.funswitch.param.UserParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;


/**
 * 用户表
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-10-19 15:41:15
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    /**
     * 内部账户存储 默认账户admin  密码frontop
     */
    private final ConcurrentHashMap<String, String> USER_CONFIG = new ConcurrentHashMap<String, String>(1) {
        {
            put("admin", "frontop");
        }
    };


    /**
     * 保存
     */
    @PostMapping("/login")
    @ApiOperation("登入")
    public Result<String> login(@RequestBody UserParam user) {
        if (USER_CONFIG.get(user.getUserName()) == null || !USER_CONFIG.get(user.getUserName()).equals(user.getPassWord())) {
            return Result.error("登入失败!");
        }
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public Result<String> update(@RequestBody UpdateUserParam user) {
        if (USER_CONFIG.get(user.getUserName()) != null || USER_CONFIG.get(user.getUserName()).equals(user.getPassWord())) {
            USER_CONFIG.put(user.getUserName(), user.getNewPassWord());
            return Result.ok();
        }
        return Result.error("修改失败!");
    }


}
